﻿var m_currentQB = null;

var rules_basic = {
    condition: 'AND',
    rules: [{
        id: 'id',
        operator: 'equal',
        value: null
    },
    {
        id: 'name',
        operator: 'equal',
        value: null
    }]

    //{
    //    condition: 'OR',
    //    rules: [{
    //        id: 'category',
    //        operator: 'equal',
    //        value: 2
    //    }, {
    //        id: 'category',
    //        operator: 'equal',
    //        value: 1
    //    }]
    //}]
};

function InitQueryBuilder(divId) {

    $('#' + divId).queryBuilder({
        plugins: ['bt-tooltip-errors'],

        filters: [{
            id: 'id',
            label: 'Id',
            type: 'integer'
        },
        {
            id: 'name',
            label: 'Name',
            type: 'string'
        }],
        //{
        //    id: 'category',
        //    label: 'Category',
        //    type: 'integer',
        //    input: 'select',
        //    values: {
        //        1: 'Books',
        //        2: 'Movies',
        //        3: 'Music',
        //        4: 'Tools',
        //        5: 'Goodies',
        //        6: 'Clothes'
        //    },
        //    operators: ['equal', 'not_equal', 'in', 'not_in', 'is_null', 'is_not_null']
        //}, {
        //    id: 'in_stock',
        //    label: 'In stock',
        //    type: 'integer',
        //    input: 'radio',
        //    values: {
        //        1: 'Yes',
        //        0: 'No'
        //    },
        //    operators: ['equal']
        //}, {
        //    id: 'price',
        //    label: 'Price',
        //    type: 'double',
        //    validation: {
        //        min: 0,
        //        step: 0.01
        //    }
        //}, {
        //    id: 'id',
        //    label: 'Identifier',
        //    type: 'string',
        //    placeholder: '____-____-____',
        //    operators: ['equal', 'not_equal'],
        //    validation: {
        //        format: /^.{4}-.{4}-.{4}$/
        //    }
        //}],

        rules: rules_basic
    });
}

function GetQueryBuilderSQL() {
    var retValue = '';

    try {
        retValue = $('#builder_JQDTable').queryBuilder('getSQL').sql;
    } catch (ex) {
        retValue = '';
    }

    return retValue;
}

function InitQueryBuilder(divId, filters, rules, width) {

    /* precarico i valori per i filtri che accettano un url come source (checkbox, radio) */
    //$.each(filters, function (i, item) {

    //    if ((item.data !== undefined) &&
    //        (item.data.url != undefined) &&
    //        ((item.input == 'checkbox') || (item.input == 'radio'))) {

    //        QBLoadAsyncValues(item, item.data.url);

    //    }
    //});

    $('#' + divId).on('afterCreateRuleInput.queryBuilder', function (e, rule) {

        if (rule.filter.plugin === 'datetimepicker') {
            var $input = rule.$el.find('.rule-value-container [name*=_value_]');
            $input.on('dp.change', function () {
                $input.trigger('change');
            });
        }

        if ((rule.filter.data !== undefined) &&
            (rule.filter.data.url != undefined) &&
            ((rule.filter.input == 'checkbox') || (rule.filter.input == 'radio'))) {

            QBLoadAsyncValues(rule.filter, rule.filter.data.url);

        }

        if ((rule.filter.data.width !== undefined) && (rule.filter.data.width > 0)){
            var theInput = rule.$el.find('.rule-value-container input');

            theInput.width = rule.filter.data.width;
        }

        if ((rule !== undefined) && (rule.filter != undefined)) {
            var theInput = rule.$el.find('.rule-value-container input');

            theInput.on('keyup', function (value) {
                QBDependsBy(rule.filter, value);
            });
        }
    });

    m_currentQB = $('#' + divId).queryBuilder({
        plugins: ['bt-tooltip-errors'],
        allow_groups: false,
        lang_code: 'it',
        condition: ['and'],
        filters: filters,
        rules: rules
    });
}

function QBDependsBy(filter, data) {
    if ((data.originalEvent != undefined) && (data.originalEvent.currentTarget != undefined)) {
        //console.log(data.originalEvent.currentTarget.value);
        alert(data.originalEvent.currentTarget.value);
    }
}

function QBLoadAsyncValues(filter, url) {
    $.ajax({
        type: "POST",
        url: url,
        data: null,
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {

            if (response !== undefined) {
                var m_values = [];

                $.each(response, function (index, value) {
                    var obj = {};
                    obj[value.value] = value.name;
                    m_values.push(obj);
                });

                filter.values = m_values;
            } else {
                filter.values = {};
            }
        },
        failure: function (response) {
            //alert(response.d);
        }
    });

}

$('#btn-reset').on('click', function () {
    $('#builder-basic').queryBuilder('reset');
});

$('#btn-set').on('click', function () {
    $('#builder-basic').queryBuilder('setRules', rules_basic);
});

$('#btn-get').on('click', function () {
    var result = $('#builder-basic').queryBuilder('getRules');

    if (!$.isEmptyObject(result)) {
        alert(JSON.stringify(result, null, 2));
    }
});